package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author chongge
 * Time 2019/10/31 14:58
 * ClassName com.itheima.controller
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Reference
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;



    @RequestMapping(value = "/getUsername",method = RequestMethod.GET)
    public Result getUsername(){
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }

    }


    /**
     * author pengboheng
     * @param username
     * @return
     */
    @GetMapping("/getmenu")
    public  Result getMenu(String username){


        try {
            List<Menu> menus =  userService.getMenus(username);

            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }


    /**  @author TanLin
     * 分页查询用户
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try {
            pageResult = userService.findPagePermission(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }

    /**
     * 添加显示角色栏
     * @return
     */
    @GetMapping("/findRoleAll")
    public Result findRoleAll(){
        List<Role> list = null;
        try {
            list = userService.findRoleAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true,null,list);
    }


    /**
     * 添加用户和角色
     * @param user
     * @param checkitemIds
     * @return
     */
    @PostMapping("/addUser")
    public Result addUser(@RequestBody com.itheima.pojo.User user,Integer[] checkitemIds){
        try {
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            userService.addUser(user,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADDUSER_FAIL);
        }
        return new Result(true,MessageConstant.ADDUSER_SUCCESS);

    }

    /**
     * 编辑回显
     * @return
     */
    @GetMapping("/findUserById")
    public Result findUserById(Integer id){
        com.itheima.pojo.User user =null;
        try {
            user =  userService.findUserById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EXCEPTION);
        }
        return new Result(true,null,user);
    }

    /**
     * 查询用户关联得角色集合
     * @param id
     * @return
     */
    @GetMapping("/findUserIdsByRoleId")
    public Result findUserIdsByRoleId(Integer id){
        List<Integer> list =null;
        try {
            list = userService.findUserIdsByRoleId(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EXCEPTION);
        }
        return new Result(true, null, list);

    }


    /**
     * 提交编辑数据
     * @param user
     * @param checkitemIds
     * @return
     */
    @PostMapping("/editUser")
    public Result editUser(@RequestBody com.itheima.pojo.User user,Integer[] checkitemIds){
        try {
//            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            userService.editUser(user,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
    }

    @GetMapping("/deleteUser")
    public Result deleteUser(Integer id){
        try {
            userService.deleteUser(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
    }



}
